package com.lvyh.lightframe.cloud.business.usercenter.sentinel;

import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 手动配置限流规则，通过Sentinel的InitFunc SPI接口来实现，只需要实现自己的InitFunc接口，并在init方法中编写规则加载的逻辑即可;
 * 还需要在resource下META-INF.services配置包路径com.lvyh.lightframe.cloud.business.usercenter.sentinel.FlowRuleInitFunc
 */
public class FlowRuleInitFunc implements InitFunc {
    @Override
    public void init() throws Exception {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setCount(2);
        rule.setResource("findRole");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setLimitApp("default");
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}
